//
//  TKIMRemindListViewCtrl.m
//  TKIM_标准版
//
//  Created by liupm on 16/5/10.
//  Copyright © 2016年 liupm. All rights reserved.
//

#import "TKIMRemindListViewCtrl.h"
#import "TKIMReminListView.h"
#import "TKIMTextBinding.h"
//#import "TKIMRoomService.h"
@interface TKIMRemindListViewCtrl ()
@property(nonatomic,strong) NSMutableArray *dataSource;
@property(nonatomic,strong) TKIMReminListView *remindListView;
@property(nonatomic,strong) TKIMLayerUtils *layerUtils;
@property(nonatomic,strong) TKIMRoomService *service;
@end

@implementation TKIMRemindListViewCtrl

- (instancetype)init
{
    self = [super init];
    if (self) {
        _service = [[TKIMRoomService alloc] init];
    }
    return self;
}

-(void)loadView
{
    [super loadView];
    self.navigationItem.title = @"选择成员";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancel)];
    _remindListView = [[TKIMReminListView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) widthDelegate:self];
    [self.view addSubview:_remindListView];
}


-(void)viewDidLoad
{
    [super viewDidLoad];
    _layerUtils = [[TKIMLayerUtils alloc] initWithContent:self.view];
    [self loadGroupMembers];
}


-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [_service cancelAllRequets];
}


/**
 *  @Author lpm, 16-05-10 15:05:45
 *
 *  @brief  请求群成员
 */
-(void)loadGroupMembers
{
    [_layerUtils showLoading:YES];
    [_service queryGroupMembers:_group.roomId callBack:^(TKIMResultVo *resultVo) {
        [_layerUtils showLoading:NO];
        if (resultVo.errorNo == 0)
        {
            _dataSource = (NSMutableArray *)resultVo.results;
            [_remindListView reloadData:_dataSource];
        }
    }];
}


/**
 *  @Author lpm, 15-10-29 13:10:46
 *
 *  @brief  跳转到聊天界面
 *
 *  @param user
 */
-(void)toChat:(TKIMUser *)user
{
    NSString *userName = [TKIMHelper getShowNameWithUser:user];
    userName = [NSString stringWithFormat:@"%@ ",userName];
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] init];
    // 拼接之前的文字
    [attributedText appendAttributedString:self.input.attributedText];
    NSAttributedString *str = [[NSAttributedString alloc] initWithString:userName];
    [attributedText replaceCharactersInRange:self.input.selectedRange withAttributedString:str];
    TKIMTextBinding *bind = [TKIMTextBinding bindingWithDeleteConfirm:YES];
    NSInteger length = self.input.selectedRange.length+self.input.selectedRange.location-1;
    NSInteger index = length?length:0;
    [attributedText addAttribute:TKIMTextBindingAttributeName value:bind range:NSMakeRange(index, str.length)];
    [attributedText addAttribute:NSFontAttributeName value:self.input.font range:NSMakeRange(0, attributedText.length)];
    NSUInteger loc = self.input.selectedRange.location+str.length;
    self.input.attributedText = attributedText;
    //将光标移动到插入的表情之情
    self.input.selectedRange = NSMakeRange(loc, 0);
    [self.navigationController popViewControllerAnimated:YES];
    _callBack(user);
}


/**
 *  @Author lpm, 16-05-10 16:05:11
 *
 *  @brief  取消
 */
-(void)cancel
{
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark 好友搜索委托

/**
 *  @Author lpm, 15-12-11 16:12:07
 *
 *  @brief
 *
 *  @param text
 *
 *  @return
 */
-(NSMutableArray *)textDidChange:(NSString *)text
{
    NSMutableArray * resultArray = [NSMutableArray array];
    //输入的文字有汉字  汉字搜索
    for (TKIMUser * user in _dataSource)
    {
        NSString *str = [TKIMHelper getShowNameWithUser:user];
        NSRange range=[str rangeOfString:text];
        if (range.length) {
            [resultArray addObject:user];
        }
    }
    return resultArray;
}

@end


